package com.knowvoyage.knowvoyageserver.controller;

import com.github.pagehelper.PageInfo;
import com.knowvoyage.knowvoyageserver.pojo.dto.FavoriteDTO;
import com.knowvoyage.knowvoyageserver.pojo.entity.Favorite;
import com.knowvoyage.knowvoyageserver.pojo.vo.FavoriteVO;
import com.knowvoyage.knowvoyageserver.pojo.vo.Result;
import com.knowvoyage.knowvoyageserver.service.FavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

@Tag(name = "收藏类", description = "收藏消息相关接口")
@Slf4j
@RestController
@RequestMapping("/favorite")
@RequiredArgsConstructor
public class FavoriteController {
    private final FavoriteService favoriteService;

    @Operation(summary = "收藏消息", description = "收藏消息接口")
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody FavoriteDTO favoriteDTO) {
        Favorite favorite = new Favorite();
        BeanUtils.copyProperties(favoriteDTO, favorite);
        log.info("插入：{}", favorite);
        return favoriteService.insert(favorite);
    }

    @Operation(summary = "根据ID查询", description = "根据ID查询接口")
    @GetMapping("/selectById")
    public Result<FavoriteVO> selectById(@RequestParam Long favoriteId) {
        return favoriteService.selectById(favoriteId);
    }

    @Operation(summary = "根据用户分页查询", description = "根据用户id分页查询接口")
    @GetMapping("/selectByUserId")
    public Result<PageInfo<FavoriteVO>> selectByUserId(@RequestParam Long userId,@RequestParam(defaultValue = "1") int pageNum,
                                                      @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<FavoriteVO> pageInfo = favoriteService.selectAllByUser(userId,pageNum,pageSize);
        return Result.success(pageInfo) ;
    }
    @Operation(summary = "删除收藏", description = "删除收藏接口")
    @GetMapping("/delete")
    public Result<Boolean> delete(@RequestParam Long favoriteId, Authentication authentication) {
        return favoriteService.delete(favoriteId, authentication);
    }

    @Operation(summary = "批量删除收藏", description = "批量删除收藏接口")
    @PostMapping("/batchDelete")
    public Result<Boolean> batchDelete(
            @RequestBody List<Long> favoriteIds,
            Authentication authentication
    ) {
        Long userId = (Long) authentication.getPrincipal();
        log.info("用户ID：{}", userId);
        log.info("收藏ID列表：{}", favoriteIds);
        return favoriteService.batchDelete(userId, favoriteIds);
    }

    @Operation(summary = "查询收藏数量", description = "查询一个用户的收藏数量接口")
    @GetMapping("/countByUserId")
    public Result<Integer> countByUserId(@RequestParam Long userId) {
        return favoriteService.countByUserId(userId);
    }

    @Operation(summary = "查询收藏数量", description = "查询某个信息被收藏的次数接口")
    @GetMapping("/countByMessageId")
    public Result<Integer> countByMessageId(@RequestParam Long messageId) {
        return favoriteService.countByMessageId(messageId);
    }

    @Operation(summary = "批量插入收藏", description = "批量插入收藏接口")
    @PostMapping("/batchInsert")
    public Result<Boolean> batchInsert(@RequestBody List<FavoriteDTO> favoriteDTOs) {
        return favoriteService.batchInsert(favoriteDTOs);
    }



}
